<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once 'common/conn.php';
require_once 'common/common.php';
require_once 'share.action.php';

$result = array();
$db = new DBC();
$usercode = getParameter("usercode");
$nickname = getParameter("nickname");
$sex = getParameter("sex");
$email = getParameter("email");
$password = getParameter("password");
$password2 = getParameter("password2");

$is_exist = false;
if (getParameter('from') == "web") {
    if ($usercode != "") {
        $sql = "SELECT deviceid FROM t_device WHERE devicecode = '" . $usercode . "' OR username = '" . $usercode . "'";
        $data = $db->fetchData($sql);
        if (checkArrayNotEmpty($data)) {
            $is_exist = true;
        }
    }
    $email_exist = false;
    $nickname_exist = false;
    $email_wrong = false;
    $passwordNotSame = false;
    $nickname_Chinese = false;
    $password_not_input = false;
    $confirm_password_not_input = false;
    $password_not_in_length = false;

    if ($email != "" && $nickname != "") {
        if (preg_match("/[\x7f-\xff]/", $nickname)) {
            $nickname_Chinese = true;
        } else if (funcemail($email)) {
            $sql = "SELECT nickname FROM t_device WHERE nickname = '" . $nickname . "'";
            $data = $db->fetchData($sql);
            if (checkArrayNotEmpty($data)) {
                $nickname_exist = true;
            } else {
                $sql = "SELECT email FROM t_device WHERE email = '" . $email . "'";
                $data = $db->fetchData($sql);
                if (checkArrayNotEmpty($data)) {
                    $email_exist = true;
                }
            }
        } else {
            $email_wrong = true;
        }
    }

    if ($password == "" || $password == "仅支持6～20位英文或数字") {
        $password_not_input = true;
    }
    if ($password2 == "" || $password2 == "仅支持6～20位英文或数字") {
        $confirm_password_not_input = true;
    }

    if (strlen($password) < 6 || strlen($password) > 20) {
        $password_not_in_length = true;
    }
    if (strlen($password2) < 6 || strlen($password2) > 20) {
        $password_not_in_length = true;
    }
    if (!isNull($password2)) {
        if ($password != $password2) {
            $passwordNotSame = true;
        }
    }
    if ($nickname_exist) {
        $result['errorNo'] = "1";
        $result['errorMsg'] = "昵称 " . $nickname . " 已经被使用。";
    } else if ($email_exist) {
        $result['errorNo'] = "1";
        $result['errorMsg'] = "邮箱 " . $email . " 已经被使用。";
    } else if (trim($email) == "" || trim($email) == "建议您使用真实邮箱地址") {
        $result['errorNo'] = "4";
        $result['errorMsg'] = "请输入邮箱。";
    } else if ($password_not_input) {
        $result['errorNo'] = "4";
        $result['errorMsg'] = "请输入密码。";
    } else if ($confirm_password_not_input) {
        $result['errorNo'] = "4";
        $result['errorMsg'] = "请输入确认密码。";
    } else if ($password_not_in_length) {
        $result['errorNo'] = "5";
        $result['errorMsg'] = "仅支持6～20位英文或数字";
    } else if (trim($nickname) == "" || trim($nickname) == "仅支持3～12位英文或数字") {
        $result['errorNo'] = "4";
        $result['errorMsg'] = "请输入昵称。";
    } else if ($email_wrong) {
        $result['errorNo'] = "4";
        $result['errorMsg'] = "邮箱 " . $email . " 格式不正确。";
    } else if ($passwordNotSame) {
        $result['errorNo'] = "3";
        $result['errorMsg'] = "两次输入密码不同";
    } else if ($nickname_Chinese) {
        $result['errorNo'] = "3";
        $result['errorMsg'] = "昵称请输出3-12位英文或数字吧";
    } else {
        if (isRegister($usercode)) {
            $result['errorNo'] = "9";
            $result['errorMsg'] = "该设备已经注册过。";
        } else {
            if ($is_exist) {
                $sql = "UPDATE t_device SET sex = " . $sex . ",password = '" . $password . "',email = '" . $email . "',nickname = '" . $nickname . "' WHERE devicecode ='" . $usercode . "'";
                $id = $db->execute($sql);
            } else {
                $sql = "INSERT INTO t_device(devicecode,sex,password,email,nickname,createTime) VALUES('" . $usercode . "'," . $sex . ",'" . $password . "','" . $email . "','" . $nickname . "',now())";
                $id = $db->insertId($sql);
            }
            if ($id > 0) {
                // 选的男headId=BBAA000,选女的headId=BBBA000
                if ($sex == 0) {
                    $sql = "UPDATE t_device SET headId = 'BBBA000' WHERE devicecode = '" . $usercode . "'";
                    $db->execute($sql);
                } else if ($sex == 1) {
                    $sql = "UPDATE t_device SET headId = 'BBAA000' WHERE devicecode = '" . $usercode . "'";
                    $db->execute($sql);
                }

                $share = array();
                $share['usercode'] = $usercode;
                $share['type'] = 1;
                $share['message'] = "我完成了注册任务。";
                updateShare($share);
                finishTask($usercode, "REGISTER");
                $result['errorNo'] = "0";
                $result['errorMsg'] = "注册成功";
            } else {
                $result['errorNo'] = "1";
                $result['errorMsg'] = "注册失败";
            }
        }
    }
    // if ($result['errorNo'] == "0") {
//    echo "<script language='javascript'>location='complete.php?errorNo=" . $result['errorNo'] . "&errorMsg=" . $result['errorMsg'] . "';</script>";
//    } else {
    //      echo "<script language='javascript'>alert('" . $result['errorMsg'] . "');</script>";
    // }
}

function isRegister($usercode) {
    $db = new DBC();
    $sql = "SELECT * FROM t_device WHERE devicecode ='" . $usercode . "' AND nickname IS NOT NULL AND password IS NOT NULL AND email IS NOT NULL";
    $data = $db->fetchData($sql);
    if (checkArrayNotEmpty($data)) {
        return true;
    }
    return false;
}

function funcemail($str) {//邮箱正则表达式
    return (preg_match('/^[_.0-9a-z-a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/', $str)) ? true : false;
}

function finishTask($usercode, $task) {
    $db = new DBC();
    $sql = "SELECT * FROM t_task_record WHERE usercode = '" . $usercode . "' AND task = '" . $task . "'";
    $task_array = $db->fetchData($sql);
    if ($task_array != false && count($task_array) > 0) {
//        $result['errorNo'] = "1"; //已经完成
//        $result['errorMsg'] = "该任务已经执行过"; //已经完成
        $result = 3;
    } else {
        $sql = "INSERT INTO t_task_record(usercode,task,createTime) VALUES('" . $usercode . "','" . $task . "',now())";
        if ($db->execute($sql)) {
//            $result['errorNo'] = "0"; //首次完成
            $result = 0;
        } else {
//            $result['errorNo'] = "1";
//            $result['errorMsg'] = "执行错误";
            $result = 1;
        }
    }
    $db->close();
    return $result;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <meta name="viewport" content="width=device-width, user-scalable=0" />
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>无标题文档</title>
        <style type="text/css">
            <!--
            body {
                background-image: url(images/bg.gif);
                margin-left: 0px;
                margin-top: 0px;
                margin-right: 0px;
                margin-bottom: 0px;
            }
            .font-style25 {
                font-family: "宋体", Arial, Helvetica, sans-serif;
                font-size: 14px;
            }
            .font-style35 {
                font-family: "宋体", Arial, Helvetica, sans-serif;
                font-size: 16px;
                font-weight: bold;
            }
            .input_word {	height: 25px;
                          width: 200px;
                          BACKGROUND-IMAGE: url(images/input_1.png);
                          BACKGROUND-REPEAT: no-repeat;
                          BORDER-LEFT-STYLE:none;
                          BORDER-RIGHT-STYLE: none;
                          BORDER-TOP-STYLE: none;
                          BORDER-bottom-STYLE: none;
                          padding-left:10px
            }
            .input_password {	height: 25px;
                              width: 200px;
                              BACKGROUND-IMAGE: url(images/input_2.png);
                              BACKGROUND-REPEAT: no-repeat;
                              BORDER-LEFT-STYLE:none;
                              BORDER-RIGHT-STYLE: none;
                              BORDER-TOP-STYLE: none;
                              BORDER-bottom-STYLE: none;
                              padding-left:10px
            }
            .c1,.c2{
                width:185px;
                line-height:240px;
            }
            .STYLE1 {color: #3366FF}
            .STYLE3 {font-family: "宋体", Arial, Helvetica, sans-serif; font-size: 16px; font-weight: bold; color: #CC0066; }
            -->
        </style>
        <script type="text/javascript">
            function radioShow(){
                var r1 = document.getElementById("r1");
                var r2 = document.getElementById("r2");
                var c1 = document.getElementById("c1");
                var c2 = document.getElementById("c2");
                if(r1.checked){
                    c1.style.display = "block";
                    c2.style.display = "none";
                }
                if(r2.checked){
                    c1.style.display = "none";
                    c2.style.display = "block";
                }
            }
            function submitCheck(){
                var email = document.getElementById("email").value;
                var password = document.getElementById("password").value;
                var password2 = document.getElementById("password2").value;
                var name = document.getElementById("name").value;
                var sex = document.getElementById("sex").value;
                var cansubmit = true;
                if(email==""){
                    alert("请输入邮箱");
                    cansubmit = false;
                }else if(password==""){
                    alert("请输入密码");
                    cansubmit = false;
                }else if(password2==""){
                    alert("请输入确认密码");
                    cansubmit = false;
                }else if(name==""){
                    alert("请输入昵称");
                    cansubmit = false;
                }else if(password!=password2){
                    alert("两次输入的密码不一样");
                    cansubmit = false;
                }else if(name.length<3||name.length>12){
                    alert("昵称小于3个字或者大于12个字");
                    cansubmit = false;
                }
                if(!cansubmit){
                    event.returnValue = false;
                }
            }
        </script>
        <script language="javascript" type="text/javascript">
            // 密码输入框JScript 文件
            function getStar(control,logValue,txt)
            {
                if(control.value==logValue)
                {
                    control.value="";
                    control.style.color="black";

                    if(control.type=="text"){
                        control.style.display = "none";
                        document.getElementById('password').style.display = "block";
                        document.getElementById('password').style.color="black";
                        document.getElementById('password').value="";
                        document.getElementById('password').focus();//加上
                    }else{
                        var pass = document.getElementById('password').value;
                        if(pass.length<1){
                            control.style.display = "none";
                            document.getElementById('log').style.display = "block";
                        }
                    }
                }
            }
            function GetEnd(control,logValue,txt)
            {
                if(control.value=="")
                {
                    control.value="仅支持6～20位英文或数字";
                    control.style.color="#666666";

                    if(control.type=="password"){
                        control.style.display = "none";
                        document.getElementById('log').style.display = "block";
                        document.getElementById('log').value="仅支持6～20位英文或数字";
                        document.getElementById('log').style.color="#666666";
                        //document.getElementById('log').focus();//加上
                    }
                    else{
                        var pass = document.getElementById('log').value;

                        if(pass.length<1){
                            control.style.display = "none";
                            document.getElementById('log').style.display = "block";
                        }
                    }
                }
            }
            function back(email,nickname,sex,usercode)
            {
                //               history.back();
                location.href = "sign.php?email="+email+"&nickname="+nickname+"&sex="+sex+"&usercode="+usercode;
            }
        </script>
    </head>
    <?php
    if (isset($result['errorMsg'])) {
        $errorMsg = trim($result['errorMsg']);
    } else {
        $errorMsg = "";
    }
    if (isset($result['errorNo'])) {
        $errorNo = trim($result['errorNo']);
    } else {
        $errorNo = "";
    }
    if ($errorNo == "0") {
        $msg = $errorMsg . "<br/><br/>请点击屏幕下方按钮返回登陆界面。";
    } else {
        $msg = "<font color=red>" . $errorMsg . "</font>";
    }
    ?>
    <body>
        <table width="528" border="0" align="center" cellpadding="0" cellspacing="0" >
            <tr>
                <td width="114" height="50" bgcolor="#FFFFFF">&nbsp;</td>
                <td width="221" bgcolor="#FFFFFF">&nbsp;</td>
                <td width="193"><img src="images/i_1.gif" width="193" height="51" /></td>
            </tr>
            <tr>
                <td><img src="images/i_2.gif" width="114" height="60" /></td>
                <td><img src="images/i_3.gif" width="221" height="60" /></td>
                <td><img src="images/j_1.jpg" width="193" height="60" /></td>
            </tr>
            <tr>
                <td colspan="3" background="images/bg_2.gif"><table width="528" border="0" align="center" cellpadding="10" cellspacing="0">
                        <tr>
                            <td><table width="100%" height="175" border="0" cellpadding="0" cellspacing="0">
                                    <tr>
                                        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                                                <tr>
                                                    <td align="center" class="font-style25"><?php echo $msg; ?></td>
                                                </tr>
                                                <tr>
                                                    <td align="center" class="font-style25">&nbsp;</td>
                                                </tr>
                                                <tr>
                                                    <td align="center" class="font-style25">&nbsp;</td>
                                                </tr>
                                                <tr>
                                                    <td align="center" class="font-style25"><?php if ($errorNo != "0") { ?><a href="#" onclick="back('<?php echo $email ?>','<?php echo $nickname; ?>','<?php echo $sex ?>','<?php echo $usercode ?>')"><img border="0" src="images/back.gif"></img></a><?php } ?></td>
                                                </tr>
                                            </table></td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table></td>
            </tr>
            <tr>
                <td colspan="3"><table width="528" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                            <td width="18"><img src="images/i_4.gif" width="18" height="18" /></td>
                            <td width="505" background="images/bg_3.gif">&nbsp;</td>
                            <td width="18"><img src="images/i_5.gif" width="18" height="18" /></td>
                        </tr>
                    </table></td>
            </tr>
        </table>
    </body>
</html>
